package com.example.lte.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * https://juejin.im/post/6882172894806278151#heading-1
 * 整合swagger3 https://juejin.im/post/6890692970018766856
 */
@Configuration
@EnableOpenApi   //swagger3用EnableOpenApi注解 ； swagger2用EnableSwagger2注解
@Profile(value = {"dev","test"})   //表示该配置只在 dev ，test 环境下生效
public class SwaggerCfg {
    @Bean
    public Docket sysCoreApi() {
        //DocumentationType.OAS_30 指的是 openApi 3.0 开放API3.0标准类型
        //Swagger 2与OpenAPI 3 的区别 https://www.jianshu.com/p/879baf1cff07
        // springfox-boot-starter 3.0.0 这个官方提供的swagger推荐使用 OAS_30标准
        // OpenAPI 规范（OAS）定义了一个标准的、语言无关的 RESTful API 接口规范，
        // 它可以同时允许开发人员和操作系统查看并理解某个服务的功能，而无需访问源代码
        // OpenAPI 规范摘要 https://www.jianshu.com/p/5365ef83252a
        return new Docket(DocumentationType.OAS_30)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.lte.api"))
                .paths(PathSelectors.any())
                .build().groupName("sys-core")
                .apiInfo(apiInfo())
                //添加登录认证
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    @Bean
    public Docket  secondBizApi() {
        return new Docket(DocumentationType.OAS_30)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.lte.second.api"))
                .paths(PathSelectors.any())
                .build().groupName("sys-biz")
                .apiInfo(sysBizInfo())
                //添加登录认证
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }




    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("LteAdmin系统核心接口文档")
                .description("LteAdmin")
                .contact(new Contact("liuZh", null, "1131201834@qq.com"))
                .version("V1.0")
                .build();
    }

    private ApiInfo sysBizInfo() {
        return new ApiInfoBuilder()
                .title("XX系统业务核心接口文档")
                .description("XXXAdmin")
                .contact(new Contact("liuZh", null, "1131201834@qq.com"))
                .version("V1.3")
                .build();
    }

    private List<SecurityScheme> securitySchemes() {
        //设置请求头信息
        List<SecurityScheme> result = new ArrayList<>();
        ApiKey apiKey = new ApiKey("Authorization", "Authorization", "header");
        result.add(apiKey);
        return result;
    }

    private List<SecurityContext> securityContexts() {
        //设置需要登录认证的路径
        List<SecurityContext> result = new ArrayList<>();
        result.add(getContextByPath("/lte/.*"));
        return result;
    }

    private SecurityContext getContextByPath(String pathRegex) {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex(pathRegex))
                .build();
    }

    private List<SecurityReference> defaultAuth() {
        List<SecurityReference> result = new ArrayList<>();
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        result.add(new SecurityReference("Authorization", authorizationScopes));
        return result;
    }


//    作者：MacroZheng
//    链接：https://juejin.im/post/6890692970018766856
//    来源：掘金
//    著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
}
